import { Component } from '@angular/core';
import { Router, Params } from '@angular/router';
import { LoginService } from '../../service/login.service';

declare let $:any;

@Component({
    selector: 'login',
    templateUrl: '../../../views/login/login.html',
    styleUrls: ['../../../css/login/login.css'],
})
export class LoginComponent {
    public mobile:any;
    public password:any;
    public isPhone :any = /^1(3|4|5|7|8)[0-9]\d{8}$/;
    constructor(
        private LoginService : LoginService,
        private router : Router
    ){
        let self : any = this;
        $.cookie("home","5");
    }
    private  uLogin(){
        let self : any = this;
        let data : any = {
            mobile: self.mobile,
            password: self.password,
        };
        self.LoginService.login({
            data: data,
            success: function (result : any) {
                console.log(result);
                if(result.error === 0){
                    let phone:any = result.mobile;
                    let id:any = result.member_id;
                    let money:any = result.money;
                    let username:any = result.username;
                    let avatar:any = result.avatar;
                    let avatar_path:any = result.avatar_path;
                    let has_mp:any = result.has_mp;
                    let token:any = result.token;
                    $.cookie('phone', phone);
                    $.cookie('id', id);
                    $.cookie('money', money);
                    $.cookie('username', username);
                    $.cookie('avatar', avatar);
                    $.cookie('avatar_path', avatar_path);
                    $.cookie('has_mp', has_mp);
                    $.cookie('token', token);
                    if (has_mp == 0){
                        self.router.navigate(['/bind_public_number']);
                    }else {
                        self.router.navigate(['/home']);
                    }
                }else{
                    console.log(result.msg);
                }
            }
        });
     }
    public submit(){
        console.log(this.mobile);
        if (this.mobile == "" || this.mobile == null){
            alert("手机号不能为空")
        }else if(this.isPhone.test(this.mobile)){
            console.log(this.mobile);
            this.uLogin();
        }else alert("手机号格式不对");
    }
    public psd_status(){
        let passwordeye = $('.psd_status');
        let showPwd = $("#inputEmail3");
        if(passwordeye.hasClass('invisible')){
            passwordeye.removeClass('invisible').addClass('visible');//密码可见
            showPwd.prop('type','text');
        }else{
            passwordeye.removeClass('visible').addClass('invisible');//密码不可见
            showPwd.prop('type','password');
        };
    }
}
